package com.huluobo.a2302mdmdemo.demo

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.LinearInterpolator
import kotlinx.coroutines.runBlocking

/**
 *  Created by LC on 2025/8/5.
 */
class MyView : View {
    //一般用于在代码中创建View,并且要给宽高才能显示出来
    constructor(context: Context) : super(context)

    //一般用于在xml中创建View,attributeSet属性的集合
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    //一般用xml中,defStyleAttr,代表一个风格文件,一般用于切换主题,黑色模式,白天模式
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    )

    //一般用xml中,defStyleRes 代表一个具体的风格文件资源,一般用于切换主题,黑色模式,白天模式
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
        context,
        attributeSet,
        defStyleAttr,
        defStyleRes
    )

    //测量
    //widthMeasureSpec 包含2个值,32位的数字,前两位代表mode(测量模式) 后30位代表size(具体大小)
    //MatchParent WarpContent 20dp -> 分别对应不同的模式 AtMost(最大值),实际的值(Exc...)
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    //摆放View的位置,一般单独的View不会使用这个方法,只有在Layout中才会使用
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    //画笔
    private lateinit var paint: Paint
    private lateinit var paintText: Paint

    init {
        initPaint()
        start()
    }

    private fun initPaint() {
        paint = Paint()
        paintText = Paint()
        paintText.color = Color.BLACK
        paint.color = Color.RED
        paintText.textSize = 100f
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
    }

    //绘制的角度
    private var progress = 0f

    private var num = 5
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        canvas.drawText("123", 100f, 100f, paint)

//        canvas.drawRect(100f, 100f, 600f, 600f, paint)
        //绘制一个范围,矩形范围
        val rectF = RectF(100f, 100f, 600f, 600f)
//        通过范围,绘制一个圆
        //1.范围 2.起始角度 3.绘制角度 4.是否使用圆心(如果true绘制完成会经过圆心相连,false,会首尾相连)
        canvas.drawArc(rectF, 270f, progress, false, paint)
        canvas.drawText(num.toString(), 320f, 375f, paintText)
    }

    fun start() {
        //协程中的new Thread
        runBlocking {
            val animator = ValueAnimator.ofFloat(0f, 360f)
            animator.addUpdateListener {
                progress = it.animatedValue as Float
                Log.e("TagA", "progress:$progress")
                //刷新自身
                invalidate()
            }
            animator.duration = 5000
            //设置插值器,可以改变动画的速率
            animator.interpolator = LinearInterpolator()
            animator.start()

            val animatorText = ValueAnimator.ofInt(5, 0)
            animatorText.addUpdateListener {
                num = it.animatedValue as Int
                invalidate()
            }
            animatorText.interpolator = LinearInterpolator()
            animatorText.duration = 5000
            animatorText.start()
        }
    }
}